package com.base.controller;

import com.base.po.voip.Rechargeablecards;
import com.base.service.LogsService;
import com.base.service.RechargeablecardsService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class index_rechargeController {
    @Autowired
    private LogsService logsService;// log日志
    @Autowired
    private RechargeablecardsService rechargeablecardsService;

    @RequestMapping("/content_recharge")
//    public @ResponseBody  Map<String, Object>
    public ModelAndView
    index(HttpServletRequest request, Integer currentPageNo, String status,
          String parentId, String userId, String orderId, String usetime1, String usetime2, String pageSize) throws Exception {
        String userid = (String) request.getSession().getAttribute("adminUserId");
        Map<String, Object> map = new HashMap<>(); // 返回数据对象
        // 设置分页 获取内容
        Page page = new Page(); // 实例化分页

        if (pageSize!=null&&pageSize!=""&&!pageSize.equals("1")){
            page.setPageSize(Integer.parseInt(pageSize));
            request.setAttribute("pageSize",pageSize);
        }else{
            request.setAttribute("pageSize",10);
        }
        Date usetime11 = null;
        Date usetime22 = null;
        if (usetime1 == null || usetime1 == "" || usetime1.equals("")) {
            usetime1 = null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            usetime11 = sdf.parse(usetime1);
        }
        if (usetime2 == null || usetime2 == "" || usetime2.equals("")) {
            usetime2 = null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            usetime22 = sdf.parse(usetime2);
        }
        if (currentPageNo != null) page.setCurrentPageNo(currentPageNo); // 设置当前页
        page.setRecordTotal(rechargeablecardsService.countByParentidusetime(userid, null, null, null, null, null, null, null, null, null, null, null, null, null, usetime11, usetime22)); // 设置总条数
        List<Rechargeablecards> r1=rechargeablecardsService.selectByParentidusetime(page, userid, null, null, null, null, null, null, null, null, null, null, null, null, null, usetime11, usetime22);
        List<String> r2=rechargeablecardsService.selectCardsCountBymoney(userid,usetime1,usetime2);
        Long r100=0L,r200=0L,r500=0L,r1000=0L,r2000=0L,r5000=0L,r10000=0L,r20000=0L;
        BigDecimal totalMoney=new BigDecimal("0.0");
        BigDecimal totalBalance=new BigDecimal("0.0");
        for(String r:r2){
            switch (Integer.parseInt(r)){
                case 100:
                    r100++;
                    break;
                case 200:
                    r200++;
                    break;
                case 500:
                    r500++;
                    break;
                case 1000:
                    r1000++;
                    break;
                case 2000:
                    r2000++;
                    break;
                case 5000:
                    r5000++;
                    break;
                case 10000:
                    r10000++;
                    break;
                case 20000:
                    r20000++;
                    break;
            }

            if (r!=null){
                totalMoney=totalMoney.add(new BigDecimal(r));
            }
        }

        request.setAttribute("exchargeList",r1);
        request.setAttribute("pages",page);
        request.setAttribute("r100",r100);
        request.setAttribute("r200",r200);
        request.setAttribute("r500",r500);
        request.setAttribute("r1000",r1000);
        request.setAttribute("r2000",r2000);
        request.setAttribute("r5000",r5000);
        request.setAttribute("r10000",r10000);
        request.setAttribute("r20000",r20000);
        request.setAttribute("totalMoney",totalMoney);
        request.setAttribute("totalBalance",totalBalance);
        request.setAttribute("usetime1",usetime1);
        request.setAttribute("usetime2",usetime2);
        return new ModelAndView("jsp/excharge", map);
//        return map;
    }



}
